<template>
  <el-form class="search-form" inline :model="searchDataForm" ref="searchFormRef" :rules="rules">
    <el-form-item label="账号">
      <el-input size="mini" v-model="searchDataForm.username" placeholder="账号" />
    </el-form-item>
    <el-form-item label="姓名">
      <el-input size="mini" v-model="searchDataForm.realname" placeholder="姓名" />
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input size="mini" v-model="searchDataForm.phone" placeholder="手机号" />
    </el-form-item>
    <el-form-item label="生日">
      <el-date-picker
        size="mini"
        type="date"
        placeholder="选择日期"
        v-model="searchDataForm.birthday"
        style="width: 100%"
      />
    </el-form-item>
    <el-form-item label="性别">
      <el-radio-group v-model="searchDataForm.sex">
        <el-radio label="1">先生</el-radio>
        <el-radio label="2">女士</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">查询数据</el-button>
      <!-- <a download="用户列表数据.xlsx" href="http://localhost:9000/users.xlsx">下载全部用户数据</a> -->
      <el-button type="success" @click="downloadUserExcel">下载全部用户数据</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { noEmptySearchObject } from '@/utils/tools'
import { exportUsersExcelApi } from '@/api/userApi'
export default {
  props: ['page', 'loadData'],
  data() {
    return {
      searchDataForm: {
        username: '',
        realname: '',
        sex: '',
        phone: '',
        birthday: ''
      },
      rules: {
        phone: [
          {
            validator: this.validatorPhone,
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    for (let key in this.$route.query) {
      this.searchDataForm[key] = this.$route.query[key]
    }
  },
  methods: {
    // 下载全部用户数据excel文件
    async downloadUserExcel() {
      let ret = await exportUsersExcelApi()
      if (ret.code === 0) {
        // window.open(ret.data)
        const aDom = document.createElement('a')
        aDom.href = ret.data
        aDom.download = true
        aDom.click()
        setTimeout(() => {
          aDom.remove()
        }, 100)
      }
    },
    validatorPhone(rule, value, callback) {
      if (value) {
        if (/^1[3-9]\d{9}$/.test(value)) {
          callback()
        } else {
          callback(new Error('手机号不合法'))
        }
      } else {
        callback()
      }
    },
    onSubmit() {
      this.$refs.searchFormRef.validate(valid => {
        // 验证通过
        if (valid) {
          let searchObj = noEmptySearchObject(this.searchDataForm)
          let query = { page: this.page }
          // 有搜索条件
          if (searchObj) {
            query = { page: this.page, ...searchObj }
          }
          this.$router.replace({ query })
          this.loadData(this.page, searchObj)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.search-form {
  background: #fff;
  padding: 10px;
  margin-top: 5px;
}
</style>
